/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.payment.common.helper;

import com.egzosn.pay.ali.bean.AliTransactionType;
import com.egzosn.pay.common.bean.*;
import com.egzosn.pay.wx.bean.WxTransactionType;
import com.medusa.gruul.common.core.auth.model.enums.Platform;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.payment.api.enums.FeeTypeEnum;
import com.medusa.gruul.payment.api.enums.PayType;
import com.medusa.gruul.payment.api.enums.WxPayEnum;
import com.medusa.gruul.payment.common.enums.INotifyResponse;
import com.medusa.gruul.payment.common.enums.NotifyResponse;
import com.medusa.gruul.payment.common.enums.TradeStatus;

/**
 * @author 张治保
 * date 2021/12/17
 */
public class PayHelper {
    private PayHelper() {
    }

    /**
     *  payChannel 支付渠道
     */
    public static Integer payChannel(PayType payType) {
        switch (payType){
            case BALANCE:
                return 0;
            case WECHAT:
                return 1;
            case ALIPAY:
                return 2;
            default:
                break;
        }
        return -1;
    }
    /**
     * tradeType 交易类型
     * {@link WxPayEnum#getType()}
     */
    public static Integer tradeType(Platform platform){
        switch (platform){
            case WECHAT_MINI_APP:
                return WxPayEnum.JSAPI_MINI.getType();
            case WECHAT_MP:
                return WxPayEnum.JSAPI_MP.getType();
            case WAP:
            case H5:
                return WxPayEnum.H5.getType();
            case APP:
                return WxPayEnum.APP.getType();
            default:
                break;
        }
        return -1;
    }

    /**
     * 交易类型，交易方式，
     * 查看{@link com.egzosn.pay.common.bean.TransactionType#getType()}及子类 ()}
     * 例如，网页支付，扫码付等等
     */
    public static TransactionType wayType(Platform platform, PayType payType) {
        if (PayType.BALANCE == payType) {
            throw new ServiceException("暂不支持当前支付");
        }
        switch (platform) {
            //app
            case APP:
                switch (payType){
                    case WECHAT:
                        return WxTransactionType.APP;
                    case ALIPAY:
                        return AliTransactionType.APP;
                    default:
                        break;
                }
            //网页端H5
            case H5:
                switch (payType) {
                    case WECHAT:
                        return WxTransactionType.MWEB;
                    case ALIPAY:
                        return AliTransactionType.PAGE;
                    default:
                        break;
                }
            //移动端H5
            case WAP:
                switch (payType) {
                    case WECHAT:
                        return WxTransactionType.MWEB;
                    case ALIPAY:
                        return AliTransactionType.WAP;
                    default:
                        break;
                }
                //其它
            case WECHAT_MINI_APP:
            case WECHAT_MP:
                return WxTransactionType.JSAPI;
            default:
                break;
        }
        throw new ServiceException("暂不支持当前支付");
    }

    /**
     * 回调响应
     */
    public static INotifyResponse notifyResponse(TradeStatus tradeStatus, PayType payType){
        switch (tradeStatus){
            case SUCCESS:
            case CLOSED:
                switch (payType){
                    case WECHAT:
                        return NotifyResponse.WECHAT_SUCCESS;
                    case ALIPAY:
                        return NotifyResponse.ALI_SUCCESS;
                    default:
                        return NotifyResponse.OTHER_SUCCESS;
                }
            case WAIT_FOR_PAY:
            default:
                break;
        }
        switch (payType){
            case WECHAT:
                return NotifyResponse.WECHAT_FAIL;
            case ALIPAY:
                return NotifyResponse.ALI_FAIL;
            default:
                return NotifyResponse.OTHER_FAIL;
        }
    }

    /**
     * 获取对应的支付货币类型
     */
    public static CurType curType(FeeTypeEnum feeType){
        if (feeType == null){
            return DefaultCurType.CNY;
        }
        switch (feeType){
            case CNY:
                return DefaultCurType.CNY;
            case USD:
                return DefaultCurType.USD;
            default:
                break;
        }
        return DefaultCurType.CNY;
    }
}
